package com.hxd.otc.module.wallet.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.wallet.bean.CoinOneKeyRateVoBean;
import com.hxd.otc.module.wallet.bean.OneKeyCoinTypeRequest;
import com.hxd.otc.module.wallet.contract.WalletCoinChangeContract;
import com.hxd.otc.module.wallet.presenter.CoinOneKeyChangePresenter;
import com.hxd.otc.other.CustomBounceTopEnter;
import com.hxd.otc.other.SlideTopExit;
import com.hxd.otc.utils.ArithUtil;
import com.hxd.otc.utils.ConvertUtil;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.utils.WalletCoinTypes;
import com.hxd.otc.widget.BlocksToast;
import com.hxd.otc.widget.LimitNumberTextWatcher;
import com.hxd.otc.widget.TitleBar;
import com.hxd.otc.widget.TypeWindow;
import com.hxd.otc.widget.WalletDialogTipsUtil;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/5 17:42
 * 描  述 :  一键换币
 */
public class CoinOneKeyChangeActivity extends BaseActivity<CoinOneKeyChangePresenter> implements WalletCoinChangeContract.View {


    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_coin_change_tips)
    TextView tvCoinChangeTips;

    @BindView(R.id.et_coin_btc_num)
    EditText etCoinBtcNum;
    @BindView(R.id.ll_btc)
    LinearLayout llBtc;
    @BindView(R.id.et_coin_ltc_num)
    EditText etCoinLtcNum;
    @BindView(R.id.et_coin_eth_num)
    EditText etCoinEthNum;
    @BindView(R.id.ll_ltc)
    LinearLayout llLtc;
    @BindView(R.id.ll_eth)
    LinearLayout llEth;
    @BindView(R.id.et_coin_aec_num)
    EditText etCoinAecNum;
    @BindView(R.id.ll_aec)
    LinearLayout llAec;
    @BindView(R.id.tv_change_coin)
    TextView tvChangeCoin;
    @BindView(R.id.tv_change_coin_num)
    TextView tvChangeCoinNum;
    @BindView(R.id.et_pay_number)
    EditText etPayNumber;
    @BindView(R.id.ll_pay_number)
    LinearLayout llPayNumber;
    @BindView(R.id.et_trade_pw)
    EditText etTradePw;
    @BindView(R.id.tv_sms_code_tips)
    TextView tvSmsCodeTips;
    @BindView(R.id.btn_change)
    Button btnChange;
    @BindView(R.id.iv_see_hide)
    ImageView ivSeeHide;
    @BindView(R.id.tv_tpw_tips)
    TextView tvTpwTips;
    @BindView(R.id.fl_see_hide)
    FrameLayout flSeeHide;
    @BindView(R.id.tv_aec_quotation)
    TextView tvAecQuotation;
    @BindView(R.id.v_pay_number_line)
    View vPayNumberLine;
    @BindView(R.id.v_tpw_line)
    View vTpwLine;
    @BindView(R.id.view_send_code_line)
    View viewSendCodeLine;
    @BindView(R.id.rl_sms)
    RelativeLayout rlSms;
    @BindView(R.id.ll_twc)
    LinearLayout llTwc;
    @BindView(R.id.ll_jpc)
    LinearLayout llJpc;
    @BindView(R.id.et_coin_twc_num)
    EditText etTwcNum;
    @BindView(R.id.et_coin_jpc_num)
    EditText etJpcNum;
    @BindView(R.id.tv_btc)
    TextView tvBtc;
    @BindView(R.id.tv_ltc)
    TextView tvLtc;
    @BindView(R.id.tv_eth)
    TextView tvEht;
    @BindView(R.id.tv_aec)
    TextView tvAec;
    @BindView(R.id.tv_twc)
    TextView tvTwc;
    @BindView(R.id.tv_jpc)
    TextView tvJpc;

    private boolean isShowPsw;

    //当前选择币pos
    private int coinSelectFlag = 0;

    //装的是coin集合
    private String[] coinParameters;

    private String[] coinParametersName;

    //aec2btcAmount BTC折合AEC数量
    private double aec2btcAmount = 0;
    //aec2ltcAmount LTC折合AEC数量
    private double aec2ltcAmount = 0;
    //aec2ethAmount ETH折合AEC数量
    private double aec2ethAmount = 0;
    private double twcAmount = 0;
    private double jpcAmount = 0;
    private double aecAmount = 0;

    private TypeWindow mTypeWindow;
    List<OneKeyCoinTypeRequest> coinTypeRequests = new ArrayList<>();
    List<CoinOneKeyRateVoBean> coinOneKeyRateVoBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet_coin_one_key_change;
    }

    @Override
    protected void initView() {
        Drawable drawable = getResources().getDrawable(R.mipmap.wallet_down_menu_icon);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        titleBar.getTitle().setCompoundDrawables(null, null, drawable, null);
        titleBar.getTitle().setCompoundDrawablePadding(DensityUtil.dp2px(10));

        //控制ui线
        vTpwLine.setVisibility(View.GONE);
        rlSms.setVisibility(View.GONE);
        viewSendCodeLine.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        coinParameters = getResources().getStringArray(R.array.wallet_change_coin_title);
        coinParametersName = getResources().getStringArray(R.array.wallet_change_coin_name);
        titleBar.getTitle().setText(coinParameters[coinSelectFlag]);
        changeUI();
        queryInfo();
        queryRate();
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initEvent() {
        titleBar.getTitle().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelectType();
            }
        });
        //转币
        btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeCoin();
            }
        });

        etCoinBtcNum.addTextChangedListener(new LimitNumberTextWatcher(etCoinBtcNum, 9, 8) {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0) {
                    if (s.toString().contains(".")) {
                        int pos = s.toString().indexOf(".");
                        if (s.toString().length() - 1 - pos > 8) {
                            //大于8位不处理
                            return;
                        }
                    }
                    try {
                        aec2btcAmount = ArithUtil.mul(ConvertUtil.convertToDouble(s.toString()),
                                coinOneKeyRateVoBeans.get(coinSelectFlag).getCoinTypeMap(
                                        coinOneKeyRateVoBeans.get(coinSelectFlag).exchangeRateVoList).get(tvBtc.getText().toString()));
                    } catch (Exception e) {
                        aec2btcAmount = 0.0;
                        e.printStackTrace();
                    }
                    updateChangeCoinNum();
                } else {
                    aec2btcAmount = 0.0;
                    updateChangeCoinNum();
                }
            }

        });
        etCoinBtcNum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    setRateInfoValue(coinParametersName[coinSelectFlag], tvBtc.getText().toString());

            }
        });
        etCoinLtcNum.addTextChangedListener(new LimitNumberTextWatcher(etCoinLtcNum, 9, 8) {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0) {
                    if (s.toString().contains(".")) {
                        int pos = s.toString().indexOf(".");
                        if (s.toString().length() - 1 - pos > 8) {
                            //大于8位不处理
                            return;
                        }
                    }
                    try {
                        aec2ltcAmount = ArithUtil.mul(ConvertUtil.convertToDouble(s.toString()), coinOneKeyRateVoBeans.get(coinSelectFlag).getCoinTypeMap(
                                coinOneKeyRateVoBeans.get(coinSelectFlag).exchangeRateVoList).get(tvLtc.getText().toString()));
                    } catch (Exception e) {
                        aec2ltcAmount = 0.0;
                        e.printStackTrace();
                    }
                    updateChangeCoinNum();
                } else {
                    aec2ltcAmount = 0.0;
                    updateChangeCoinNum();
                }
            }
        });
        etCoinLtcNum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    setRateInfoValue(coinParametersName[coinSelectFlag], tvLtc.getText().toString());
            }
        });
        etCoinEthNum.addTextChangedListener(new LimitNumberTextWatcher(etCoinEthNum, 9, 8) {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0) {
                    if (s.toString().contains(".")) {
                        int pos = s.toString().indexOf(".");
                        if (s.toString().length() - 1 - pos > 8) {
                            //大于8位不处理
                            return;
                        }
                    }
                    try {
                        aec2ethAmount = ArithUtil.mul(ConvertUtil.convertToDouble(s.toString()), coinOneKeyRateVoBeans.get(coinSelectFlag).getCoinTypeMap(
                                coinOneKeyRateVoBeans.get(coinSelectFlag).exchangeRateVoList).get(tvEht.getText().toString()));
                    } catch (Exception e) {
                        aec2ethAmount = 0.0;
                        e.printStackTrace();
                    }
                    updateChangeCoinNum();
                } else {
                    aec2ethAmount = 0.0;
                    updateChangeCoinNum();
                }
            }

        });

        etCoinEthNum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    setRateInfoValue(coinParametersName[coinSelectFlag], tvEht.getText().toString());
            }
        });
        etCoinAecNum.addTextChangedListener(new LimitNumberTextWatcher(etCoinAecNum, 9, 8) {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0) {
                    if (s.toString().contains(".")) {
                        int pos = s.toString().indexOf(".");
                        if (s.toString().length() - 1 - pos > 8) {
                            //大于8位不处理
                            return;
                        }
                    }

                    try {
                        aecAmount = ArithUtil.mul(ConvertUtil.convertToDouble(s.toString()), coinOneKeyRateVoBeans.get(coinSelectFlag).getCoinTypeMap(
                                coinOneKeyRateVoBeans.get(coinSelectFlag).exchangeRateVoList).get(tvAec.getText().toString()));
                    } catch (Exception e) {
                        aecAmount = 0.0;
                        e.printStackTrace();
                    }
                    updateChangeCoinNum();
                } else {
                    aecAmount = 0.0;
                    updateChangeCoinNum();
                }
            }

        });

        etCoinAecNum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    setRateInfoValue(coinParametersName[coinSelectFlag], tvAec.getText().toString());
            }
        });

        etTwcNum.addTextChangedListener(new LimitNumberTextWatcher(etCoinAecNum, 9, 8) {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0) {
                    if (s.toString().contains(".")) {
                        int pos = s.toString().indexOf(".");
                        if (s.toString().length() - 1 - pos > 8) {
                            //大于8位不处理
                            return;
                        }
                    }
                    try {
                        twcAmount = ArithUtil.mul(ConvertUtil.convertToDouble(s.toString()), coinOneKeyRateVoBeans.get(coinSelectFlag).getCoinTypeMap(
                                coinOneKeyRateVoBeans.get(coinSelectFlag).exchangeRateVoList).get(tvTwc.getText().toString()));
                    } catch (Exception e) {
                        twcAmount = 0.0;
                        e.printStackTrace();
                    }
                    updateChangeCoinNum();
                } else {
                    twcAmount = 0.0;
                    updateChangeCoinNum();
                }
            }

        });

        etTwcNum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    setRateInfoValue(coinParametersName[coinSelectFlag], tvTwc.getText().toString());
            }
        });
        etJpcNum.addTextChangedListener(new LimitNumberTextWatcher(etCoinAecNum, 9, 8) {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0) {
                    if (s.toString().contains(".")) {
                        int pos = s.toString().indexOf(".");
                        if (s.toString().length() - 1 - pos > 8) {
                            //大于8位不处理
                            return;
                        }
                    }

                    try {
                        jpcAmount = ArithUtil.mul(ConvertUtil.convertToDouble(s.toString()), coinOneKeyRateVoBeans.get(coinSelectFlag).getCoinTypeMap(
                                coinOneKeyRateVoBeans.get(coinSelectFlag).exchangeRateVoList).get(tvJpc.getText().toString()));
                    } catch (Exception e) {
                        jpcAmount = 0.0;
                        e.printStackTrace();
                    }
                    updateChangeCoinNum();
                } else {
                    jpcAmount = 0.0;
                    updateChangeCoinNum();
                }
            }

        });
        etJpcNum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    setRateInfoValue(coinParametersName[coinSelectFlag], tvJpc.getText().toString());
            }
        });
        etTradePw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        //没设置交易密码点击edittext弹出对话框
        etTradePw.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                /*return true不会响应键盘打开事件，但是有响应触摸事件
                也就是说在响应触摸按下和松开后，事件就被消费而不往下传递了
                return false时，在触摸被松开的时候响应打开键盘事件，
                如果触摸长时间被按下，触摸松开时也不会对其他时间进行相应
                例如，不会打开系统默认键盘。
                */
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_UP:
                        if (!UserHelper.isSetPayPassword()) {
                            WalletDialogTipsUtil.showTpwTipsDialog(CoinOneKeyChangeActivity.this);
                        }
                        break;
                }
                return false;
            }
        });
        flSeeHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isShowPsw = !isShowPsw;
                if (isShowPsw) {
                    ivSeeHide.setBackgroundResource(R.mipmap.wallet_according_icon);
                    etTradePw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    ivSeeHide.setBackgroundResource(R.mipmap.wallet_hidden_icon);
                    etTradePw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                if (!TextUtils.isEmpty(etTradePw.getText())) {
                    etTradePw.setSelection(etTradePw.getText().length());
                }
            }
        });

        //默认不显示密码
        isShowPsw = true;
        flSeeHide.performClick();

    }

    private void updateChangeCoinNum() {
        switch (coinSelectFlag) {
            case 0:
                tvChangeCoinNum.setText(String.format("%s", ArithUtil.add(aec2btcAmount, aec2ltcAmount, aec2ethAmount, twcAmount, jpcAmount)));
                break;
            case 3:
                tvChangeCoinNum.setText(String.format("%s", ArithUtil.add(aecAmount, twcAmount, jpcAmount)));
                break;
            case 1:
                tvChangeCoinNum.setText(String.format("%s", ArithUtil.add(aecAmount, jpcAmount)));
                break;
            case 2:
                tvChangeCoinNum.setText(String.format("%s", ArithUtil.add(aecAmount, twcAmount)));
                break;
        }

    }

    //充值类型
    private void showSelectType() {
        mTypeWindow = new TypeWindow(this, coinParameters, titleBar.getTitle().getText().toString(), titleBar.getTitle().getWidth());
        mTypeWindow.setOnItemClickListener(new TypeWindow.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mTypeWindow.superDismiss();
                if (position == coinSelectFlag) return;//点击相同的不刷新
                coinSelectFlag = position;
                titleBar.getTitle().setText(coinParameters[coinSelectFlag]);
                if (coinSelectFlag == 0) {//如果下拉选中AEC，则 汇率显示 BTC/AEC
                    setRateInfoValue(coinParametersName[coinSelectFlag], tvBtc.getText().toString());
                } else {// 否则 汇率显示AEC/下拉选中的币种
                    setRateInfoValue(coinParametersName[coinSelectFlag], tvAec.getText().toString());
                }
                changeUI();
                setBanlance();
                clearData();
            }
        });
        mTypeWindow
                .offset(DensityUtil.px2dp((getWindowManager().getDefaultDisplay().getWidth()
                        - titleBar.getTitle().getWidth()) / 2), 0)
                .anchorView(titleBar)
                .gravity(Gravity.BOTTOM)
                .showAnim(new CustomBounceTopEnter())
                .dismissAnim(new SlideTopExit())
                .dimEnabled(false)
                .show();
        Drawable drawable = getResources().getDrawable(R.mipmap.wallet_up_menu_icon);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        titleBar.getTitle().setCompoundDrawables(null, null, drawable, null);
        titleBar.getTitle().setCompoundDrawablePadding(DensityUtil.dp2px(10));
        mTypeWindow.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                Drawable drawable = getResources().getDrawable(R.mipmap.wallet_down_menu_icon);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                titleBar.getTitle().setCompoundDrawables(null, null, drawable, null);
                titleBar.getTitle().setCompoundDrawablePadding(DensityUtil.dp2px(10));
            }
        });
    }

    //aec ，msc ui切换
    private void changeUI() {
        switch (coinSelectFlag) {
            case 0://AEC
                tvCoinChangeTips.setText(R.string.wallet_change_coin_aec_tips);
                tvChangeCoin.setText(R.string.wallet_change_coin_aec_nums);
                llBtc.setVisibility(View.VISIBLE);
                llLtc.setVisibility(View.VISIBLE);
                llEth.setVisibility(View.VISIBLE);
                llAec.setVisibility(View.GONE);
                llTwc.setVisibility(View.VISIBLE);
                llJpc.setVisibility(View.VISIBLE);
                break;
            case 3://msc
                tvCoinChangeTips.setText(R.string.wallet_change_coin_msc_tips);
                tvChangeCoin.setText(R.string.wallet_change_coin_msc_nums);
                llBtc.setVisibility(View.GONE);
                llLtc.setVisibility(View.GONE);
                llEth.setVisibility(View.GONE);
                llAec.setVisibility(View.VISIBLE);
                llTwc.setVisibility(View.VISIBLE);
                llJpc.setVisibility(View.VISIBLE);
                break;
            case 1://twc
                llTwc.setVisibility(View.GONE);
                llJpc.setVisibility(View.VISIBLE);
                tvCoinChangeTips.setText(R.string.wallet_change_coin_twc_tips);
                tvChangeCoin.setText(R.string.wallet_change_coin_twc_nums);
                llBtc.setVisibility(View.GONE);
                llLtc.setVisibility(View.GONE);
                llEth.setVisibility(View.GONE);
                llAec.setVisibility(View.VISIBLE);
                break;
            case 2://jpc
                llTwc.setVisibility(View.VISIBLE);
                llJpc.setVisibility(View.GONE);
                tvCoinChangeTips.setText(R.string.wallet_change_coin_jpc_tips);
                tvChangeCoin.setText(R.string.wallet_change_coin_jpc_nums);
                llBtc.setVisibility(View.GONE);
                llLtc.setVisibility(View.GONE);
                llEth.setVisibility(View.GONE);
                llAec.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void clearData() {
        etCoinLtcNum.setText("");
        etCoinBtcNum.setText("");
        etCoinEthNum.setText("");
        etCoinAecNum.setText("");
        etJpcNum.setText("");
        etTwcNum.setText("");

        etCoinLtcNum.clearFocus();
        etCoinEthNum.clearFocus();
        etJpcNum.clearFocus();
        etTwcNum.clearFocus();

        etTradePw.setText("");
        tvChangeCoinNum.setText("");

        aec2btcAmount = 0;
        aec2ltcAmount = 0;
        aec2ethAmount = 0;
    }

    private void changeCoin() {
        switch (coinSelectFlag) {
            case 0:
                checkDataAndCoinChangeAEC();
                break;
            case 1:
                checkDataAndAECChangeMSC();
                break;
            case 2:
                checkDataAndAECChangeMSC();
                break;
            case 3:
                checkDataAndAECChangeMSC();
                break;
        }
    }

    //AEC转MSC
    private void checkDataAndAECChangeMSC() {
        coinTypeRequests.clear();
        if (TextUtils.isEmpty(tvChangeCoinNum.getText().toString())) {
            BlocksToast.get().showToast(R.string.wallet_toast_change_coin_error);
            return;
        }

        switch (coinSelectFlag) {
            case 1://twc
                if (setOneCoinTypeRequestData(etCoinAecNum.getText().toString(), tvAec.getText().toString().trim()))
                    return;
                if (setOneCoinTypeRequestData(etJpcNum.getText().toString(), tvJpc.getText().toString().trim()))
                    return;
                break;
            case 2://jpc
                if (setOneCoinTypeRequestData(etCoinAecNum.getText().toString(), tvAec.getText().toString().trim()))
                    return;
                if (setOneCoinTypeRequestData(etTwcNum.getText().toString(), tvTwc.getText().toString().trim()))
                    return;
                break;
            case 3://MSC
                if (setOneCoinTypeRequestMscData(etCoinAecNum, etCoinAecNum.getText().toString(), mAECMinMultiple, tvAec.getText().toString().trim()))
                    return;
                if (setOneCoinTypeRequestMscData(etTwcNum, etTwcNum.getText().toString(), mTWCMinMultiple, tvTwc.getText().toString().trim()))
                    return;
                if (setOneCoinTypeRequestMscData(etJpcNum, etJpcNum.getText().toString(), mJPCMinMultiple, tvJpc.getText().toString().trim()))
                    return;
                break;
        }
        if (TextUtils.isEmpty(etTradePw.getText().toString().trim())) {
            BlocksToast.get().showToast(R.string.wallet_toast_withdraw_empty_tpw);
            return;
        }
        mPresenter.oneCoinTypeChange(coinParametersName[coinSelectFlag], etTradePw.getText().toString().trim(), coinTypeRequests);
    }

    //换AEC
    private void checkDataAndCoinChangeAEC() {
        coinTypeRequests.clear();
        if (TextUtils.isEmpty(tvChangeCoinNum.getText().toString())) {
            BlocksToast.get().showToast(R.string.wallet_toast_change_coin_error);
            return;
        }

        if (setOneCoinTypeRequestData(etCoinBtcNum.getText().toString(), tvBtc.getText().toString().trim()))
            return;
        if (setOneCoinTypeRequestData(etCoinLtcNum.getText().toString(), tvLtc.getText().toString().trim()))
            return;
        if (setOneCoinTypeRequestData(etCoinEthNum.getText().toString(), tvEht.getText().toString().trim()))
            return;
        if (setOneCoinTypeRequestData(etTwcNum.getText().toString(), tvTwc.getText().toString().trim()))
            return;
        if (setOneCoinTypeRequestData(etJpcNum.getText().toString(), tvJpc.getText().toString().trim()))
            return;

        if (TextUtils.isEmpty(etTradePw.getText().toString().trim())) {
            BlocksToast.get().showToast(R.string.wallet_toast_withdraw_empty_tpw);
            return;
        }
        mPresenter.oneCoinTypeChange(coinParametersName[coinSelectFlag], etTradePw.getText().toString().trim(), coinTypeRequests);
    }

    /**
     * 设置每个输入框的校验  ，如果数据框有数据，则将太币种加入 请求数据集
     *
     * @param coinNum
     * @param coinType
     * @return
     */
    private boolean setOneCoinTypeRequestData(String coinNum, String coinType) {
        if (!TextUtils.isEmpty(coinNum)) {
            if (WalletCoinTypes.getInstance().getBalance(coinType) < Double.valueOf(coinNum)) {
                BlocksToast.get().showToast(getString(R.string.wallet_toast_change_coin_ple_not_enough_nums, coinType));
                return true;
            }
            OneKeyCoinTypeRequest coinTypeRequest = new OneKeyCoinTypeRequest();
            coinTypeRequest.coinType = coinType;
            coinTypeRequest.amount = Double.valueOf(coinNum);
            coinTypeRequests.add(coinTypeRequest);
        }
        return false;
    }

    /**
     * 校验换msc 的输入框，当 不是输入框的整数倍时， 自动换算成整数倍
     *
     * @param etCoin
     * @param coinNum
     * @param multiple
     * @param coinType
     * @return
     */
    private boolean setOneCoinTypeRequestMscData(EditText etCoin, String coinNum, int multiple, String coinType) {
        if (!TextUtils.isEmpty(coinNum)) {
            if (WalletCoinTypes.getInstance().getBalance(coinType) < Double.valueOf(coinNum)) {
                BlocksToast.get().showToast(getString(R.string.wallet_toast_change_coin_ple_not_enough_nums, coinType));
                return true;
            }
            try {
                if ((Long.valueOf(coinNum) % multiple) != 0) {
                    etCoin.setText(getString(R.string.one_placeholder2, (Long.valueOf(coinNum) / multiple) * multiple));
                    BlocksToast.get().showToast(String.format(getString(R.string.wallet_please_enter_integer_multiples_of_number), multiple));
                    return true;
                }
            } catch (Exception e) {
                etCoin.setText("0");
                e.printStackTrace();
            }
            OneKeyCoinTypeRequest coinTypeRequest = new OneKeyCoinTypeRequest();
            coinTypeRequest.coinType = coinType;
            coinTypeRequest.amount = Double.valueOf(coinNum);
            coinTypeRequests.add(coinTypeRequest);
        }
        return false;
    }


    @Override
    protected CoinOneKeyChangePresenter createPresenter() {
        return new CoinOneKeyChangePresenter(this);
    }

    //查询详情

    private void queryInfo() {
        setBanlance();
    }

    private int mAECMinMultiple = 10000;//aec转msc最小倍数
    private int mTWCMinMultiple = 1;//twc
    private int mJPCMinMultiple = 1;//jpc

    //显示可用余额
    private void setBanlance() {
        switch (coinSelectFlag) {
            case 0:
                etCoinBtcNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                        + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.btc))));
                etCoinLtcNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                        + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.ltc))));
                etCoinEthNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                        + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.eth))));
                etTwcNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                        + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.twc))));
                etJpcNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                        + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.jpc))));
                break;
            case 3:
                etCoinAecNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                        + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.aec)))
                        + String.format(getString(R.string.wallet_please_enter_integer_multiples_of_number_hint), mAECMinMultiple + ""));

                if (coinOneKeyRateVoBeans == null)
                    return;
                List<CoinOneKeyRateVoBean.WalletCoinRateData> exchangeRateVoList = null;
                CoinOneKeyRateVoBean selecedCoinOneKeyRateVoBean = null;
                for (CoinOneKeyRateVoBean coinOneKeyRateVoBean : coinOneKeyRateVoBeans) {
                    if ("MSC_1".equals(coinOneKeyRateVoBean.coinType)) {
                        selecedCoinOneKeyRateVoBean = coinOneKeyRateVoBean;
                        exchangeRateVoList = coinOneKeyRateVoBean.exchangeRateVoList;
                        break;
                    }
                }
                if (selecedCoinOneKeyRateVoBean == null) return;
                Map<String, Double> coinTypeMap = selecedCoinOneKeyRateVoBean.getCoinTypeMap(exchangeRateVoList);

                if (coinTypeMap.containsKey(getString(R.string.twc))) {
                    mTWCMinMultiple = (int) (mAECMinMultiple * coinTypeMap.get(getString(R.string.twc)));
                    etTwcNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                            + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.twc)))
                            + String.format(getString(R.string.wallet_please_enter_integer_multiples_of_number_hint), mTWCMinMultiple + ""));
                }
                if (coinTypeMap.containsKey(getString(R.string.jpc))) {
                    mJPCMinMultiple = (int) (mAECMinMultiple * coinTypeMap.get(getString(R.string.jpc)));
                    etJpcNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                            + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.jpc)))
                            + String.format(getString(R.string.wallet_please_enter_integer_multiples_of_number_hint), mJPCMinMultiple + ""));
                }
                break;
            case 1:
                etJpcNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " " + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.jpc))));
                etCoinAecNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                        + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.aec))));
                break;
            case 2:
                etTwcNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " " + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.twc))));
                etCoinAecNum.setHint(getString(R.string.wallet_quick_withdraw_nums_hint_tips) + " "
                        + StringUtil.format8Number(WalletCoinTypes.getInstance().getBalance(getString(R.string.aec))));
                break;

        }

    }

    //查询汇率
    private void queryRate() {
        if (mPresenter != null) {
            mPresenter.getWalletCoinRate();
        }
    }

    //查询汇率
    @Override
    public void getWalletCoinRateSuccess(List<CoinOneKeyRateVoBean> datas) {
        coinOneKeyRateVoBeans = datas;
        setRateInfoValue(coinParametersName[coinSelectFlag], tvBtc.getText().toString());
        clearData();
    }

    //设置汇率
    private void setRateInfoValue(String coinTypeOne, String coinTypeTwo) {
        if (coinOneKeyRateVoBeans != null) {
            Map<String, Double> coinTypeMap = coinOneKeyRateVoBeans.get(coinSelectFlag)
                    .getCoinTypeMap(coinOneKeyRateVoBeans.get(coinSelectFlag).exchangeRateVoList);
            if (coinTypeMap.containsKey(coinTypeTwo)) {
                tvAecQuotation.setText(getString(R.string.two_placeholder_point, coinTypeTwo + "/" + coinTypeOne,
                        StringUtil.formatNumber(coinTypeMap.get(coinTypeTwo), 8, true)));
            }
        }
    }

    @Override
    public void SmsSendSuccess() {

    }

    @Override
    public void changeCoinSuccess() {
        showToast(getString(R.string.wallet_toast_change_coin_success));
        clearData();
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void setSMSEnable(boolean b) {
    }

    @Override
    public void setTransferEnable(boolean b) {
        btnChange.setEnabled(b);
    }

    @Override
    public void onShowLoading() {
        showSpinnerDlg(null, false);
    }

    @Override
    public void onHideLoading() {
        dismissSpinnerDlg();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UIUtils.dismissDialog(mTypeWindow);
    }
}
